import axios from 'axios'
import {getToken} from "@utils/auth";
import {showMessage} from "@utils/message"

const service = axios.create({
    baseURL: import.meta.env.VITE_API_BASE,
    timeout: 15000
})

// 请求拦截
service.interceptors.request.use(config => {
    config.headers['X-Token'] = getToken()
    return config
})

// 响应拦截
service.interceptors.response.use(
    response => response.data,
    error => {
        showMessage(error.response?.data?.message || '请求失败')
        return Promise.reject(error)
    }
)

export const get = (url, params) => service.get(url, {params})
export const post = (url, data) => service.post(url, data)